//设置cookie
function setCookie(key, value, date, path, domain, secure) {
    var cookieStr = "";
    if (key && value) {
        cookieStr += encodeURIComponent(key) + "=" + encodeURIComponent(value) + ";";
    }
    if (date instanceof Date) {
        // expires 如果这个单词写错，在设置cookie 显示时间的地方，只会 session
        cookieStr += "expires=" + date + ";";
    }
    if (path) {
        cookieStr += "path=" + path + ";";
    }
    if (domain) {
        cookieStr += "domain=" + domain + ";";
    }
    if (secure) {
        cookieStr += "secure";
    }
    document.cookie = cookieStr;
    return document.cookie;
}
//获取所有的cookie
function getCookieAll() {
    var result = {};
    var str = document.cookie; // 1  aaa=bbb;ccc=ddd    2.  aaa=ccc

    if (str.indexOf(";") != -1) {
        //2+
        var doubleArr = str.split(";");  // [aaa=bbb,ccc=ddd]
        for (var i = 0; i < doubleArr.length; i++) {
            if (doubleArr[i].indexOf("=") != -1) {
                var arr = doubleArr[i].split("=");  // [aaa,bbb]  [ccc,ddd]
                result[decodeURIComponent(arr[0].trim())] = decodeURIComponent(arr[1]);
            }
        }
    } else {
        // 1    aaa=ccc
        if (str.indexOf("=") != -1) {
            var arr = str.split("=");
            result[decodeURIComponent(arr[0])] = decodeURIComponent(arr[1]);
        }
    }
    return result;
}

//获取cookie
function getCookieByKey(key) {
    var result = getCookieAll(); // 返回  对象{}
    return result[key];
}
//移除cookie
function removeCookie(key) {
    var date = new Date();
    date.setDate(date.getDate() - 10000);
    document.cookie = key + "= ;expires=" + date;
}


